﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class PowerWarning_Weight_Edit : System.Web.UI.Page
{
   private string sEditStatus = "1";
   private WeightBLL bll = new WeightBLL();
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sID = this.Request.QueryString["ID"].ToString().Trim();
        }
        catch
        { }
        if (!IsPostBack)
        {
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

                if (sEditStatus == "2")
                {
                    Weight model = bll.GetModel(int.Parse(sID));
                    if (model != null)
                    {
                        sltYear.Value = model.Years;
                        txtWeight1.Text = model.Weight1.ToString();
                        txtWeight2.Text = model.Weight2.ToString();
                        txtWeight3.Text = model.Weight3.ToString();
                        txtWeight4.Text = model.Weight4.ToString();
                    }
                }
            }
            catch
            {

            }
            sltYear.Value = DateTime.Now.Year.ToString();
        }
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        Weight model = new Weight();
        try
        {
            model.Years = sltYear.Value;
            model.Weight1 = Decimal.Parse(txtWeight1.Text.Trim());
            model.Weight2 = Decimal.Parse(txtWeight2.Text.Trim());
            model.Weight3 = Decimal.Parse(txtWeight3.Text.Trim());
            model.Weight4 = Decimal.Parse(txtWeight4.Text.Trim());
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                if (bll.Exists(" Years='" + model.Years + "'"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                int iback = bll.Add(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                model.W_ID = this.Request.QueryString["ID"].ToString().Trim() == "" ? 0 : int.Parse(this.Request.QueryString["ID"].ToString().Trim());
                if (bll.Exists(" Years='" + model.Years + "'and W_ID<>'" + model.W_ID + "'"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                int iback = bll.Update(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!');</script>");
            return;
        }
    }
}
